package com.molichuxing.services.business.service.impl;

import com.molichuxing.framework.utils.CollectionUtils;
import com.molichuxing.services.business.dto.response.GroupBindDesenseDto;
import com.molichuxing.services.business.service.DesenseBizService;
import com.molichuxing.services.infrastructure.dto.response.response.DesenseDto;
import com.molichuxing.services.infrastructure.dto.response.response.GroupDesenseDto;
import com.molichuxing.services.infrastructure.service.DesenseService;
import com.molichuxing.services.infrastructure.service.GroupDesenseService;
import org.apache.commons.collections.map.HashedMap;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service("desenseBizService")
public class DesenseBizServiceImpl implements DesenseBizService {

    @Resource
    private GroupDesenseService groupDesenseService;

    @Resource
    private DesenseService desenseService;

    /**
     * 根据角色id获取角色绑定的脱敏字段
     * @param groupId
     * @return
     */
    @Override
    public List<DesenseDto> getDesenseByGroupId(int groupId) {
        if (groupId <= 0) {
            throw new IllegalArgumentException("param groupId invalid groupId is 0");
        }

        GroupBindDesenseDto result = new GroupBindDesenseDto();

        //获取中间表数据
        Map<String, Object> getGroupDesenseParam = new HashedMap();
        getGroupDesenseParam.put("groupId", groupId);
        List<GroupDesenseDto> groupDesenseList = groupDesenseService.getGroupDesenseListByParam(getGroupDesenseParam);

        Map<Integer, Integer> groupBindDesenseMap = new HashMap<>();
        final List<Integer> desenseIdList = new ArrayList<>();
        if (CollectionUtils.isNotEmpty(groupDesenseList)) {
            groupDesenseList.forEach(groupDesense -> {
                groupBindDesenseMap.put(groupDesense.getDesenseId(), groupDesense.getDesenseId());
            });
        }

        //获取所有启动状态的数据
        Map<String, Object> getAllDesenseParam = new HashMap<>();
        getAllDesenseParam.put("status", Boolean.FALSE); //false=启用
        List<DesenseDto> dataList = desenseService.getDesenseListByParam(getAllDesenseParam);
        if (CollectionUtils.isNotEmpty(dataList)) {
            dataList.forEach(data -> {
                if (groupBindDesenseMap.get(data.getId()) != null) {
                    data.setBind(Boolean.TRUE);
                }
            });
        }
        return dataList;
    }
}
